<script setup>
import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
import { onLoad, onShow } from '@dcloudio/uni-app'

import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import optionDeptPerson from '@/store/modules/optionDeptPerson'
import {  addFinancialApplyControllerApi } from '@/api/attendance'
import { getUserProfile } from '@/api/system/user'
import approvalProcessState from '@/store/modules/approvalProcess'
import { isObjectValuesNotEmpty } from '@/utils/objIsnull.js'
// 弃用
const title = '采购申请'

const entertainDateRef = ref(null)
const ItemRequisitionList = ref({
  date: '',
  reason: '',
	purchaseType:'',
  itemRequisitionDetail: [
    {
      goodsName: '',
      goodsModel: '',
      goodsNumber: '',
      goodsUnit: '',
			goodsUnitPrice:'',
			goodsMoney:'',
      goodsPurpose: '',
    },
  ],
	purchaseMoney:'',
	remark:''
})
const userId = ref(null)
const deptId = ref(null)
let attchment = ref([])
const getDesignDrawingsEmit = (emit) => {
  attchment.value = emit
}
const clickSave = async () => {
	const RequiredItemList = JSON.parse(JSON.stringify(ItemRequisitionList.value))
	delete RequiredItemList.remark
	console.log(RequiredItemList,'RequiredItemList')
	if(isObjectValuesNotEmpty(RequiredItemList)){
		let params = {
			purchaseNature:ItemRequisitionList.value.purchaseType,
		  date:dayjs(ItemRequisitionList.value.date).format('YYYY-MM-DD hh:mm:ss'),
			reason:ItemRequisitionList.value.reason,
		  details:JSON.stringify(ItemRequisitionList.value.itemRequisitionDetail),
			money:ItemRequisitionList.value.purchaseMoney,
			remark:ItemRequisitionList.value.remark,
		  ruleId:'caigou',
		  type:'采购申请',
		  attchment:JSON.stringify(attchment.value),
		  userId: userId.value,
			deptId: deptId.value,
		  approve: approvalProcessState.state.approvalProcess
		}
		console.log(params,'采购申请提交params')
		let { code } = await addFinancialApplyControllerApi(params)
		if (code === 200) {
		  uni.showToast({
		    title: '提交成功',
		    duration: 1500,
		    icon: 'success',
		  })
		  setTimeout(() => {
		    uni.navigateBack()
		  }, 1000)
		}
	}else{
		uni.showToast({
			title:'请输入完整数据',
			icon:'error'
		})
	}
 
}
const goHistoryView = ()=>{
  uni.navigateTo({
    url:'/components/TypeDefineModel?type=采购申请&apiName=财务'
  })
}
const purchaseTypeRef = ref(null)
const purchaseTypeNumParams = ref(null)
const purchaseTypeColumns = [['办公用品', '生产用料','其他']]
const purchaseTypeConfirm = (e) => {
  ItemRequisitionList.value.purchaseType = e.value[0]
	// 审批绑定的费用承担部门  传参
	purchaseTypeNumParams.value = e.indexs[0] + 1
}
const purchaseTypeFn = () => {
  purchaseTypeRef.value.open()
}

const formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`
  }
  if (type === 'month') {
    return `${value}月`
  }
  if (type === 'day') {
    return `${value}日`
  }
  if (type === 'hour') {
    return `${value}时`
  }
  if (type === 'minute') {
    return `${value}分`
  }
  return value
}

const entertainDateFn = () => {
  entertainDateRef.value.open()
}
let entertainDatePicker = dayjs()
const entertainDateConfirm = (obj) => {
  ItemRequisitionList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
}


const copyDetailItem = ()=>{
  ItemRequisitionList.value.itemRequisitionDetail.push(
    {
      goodsName: '',
      goodsModel: '',
      goodsNumber: '',
      goodsUnit: '',
      goodsUnitPrice: '',
      goodsMoney: '',
      goodsPurpose: '',
    }
  )
}
const deleteDetailItem = (index)=>{
  ItemRequisitionList.value.itemRequisitionDetail.splice(index,1)
}
const getUserProfileFn = async () => {
  let { code, data } = await getUserProfile()
  if (code === 200) {
    userId.value = data.userId
		deptId.value = data.deptId
  }
}

onLoad( ()=>{
  getUserProfileFn()
})
</script>

<template>
  <view>
    <!--   采购申请  -->
    <HeaderNavTabarSvg :title="title">
      <image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
    </HeaderNavTabarSvg>

    <view class="textarea-box">
      <text class="info-list-left isrequired">申请事由</text>
      <textarea
        v-model="ItemRequisitionList.reason"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>
		
		<view class="picker-content" @tap="purchaseTypeFn">
		  <view class="measurement-info-list">
		    <text class="info-list-left isrequired">采购类型</text>
		    <view class="info-list-right">
		      <view class="uni-input" v-if="ItemRequisitionList.purchaseType"
		        >{{ ItemRequisitionList.purchaseType }}
		      </view>
		      <view class="uni-inputs" v-else>请选择</view>
		      <i class="iconfont icon-youjiantou"></i>
		    </view>
		  </view>
		</view>
		<uv-picker
		  ref="purchaseTypeRef"
		  :columns="purchaseTypeColumns"
		  @confirm="purchaseTypeConfirm"
		></uv-picker>
		
		<view class="picker-content" @tap="entertainDateFn">
		  <view class="measurement-info-list">
		    <text class="info-list-left isrequired">期望交付日期</text>
		    <view class="info-list-right">
		      <view class="uni-input" v-if="ItemRequisitionList.date"
		        >{{ ItemRequisitionList.date }}
		      </view>
		      <view class="uni-inputs" v-else>请选择</view>
		      <i class="iconfont icon-youjiantou"></i>
		    </view>
		  </view>
		</view>
		<uv-datetime-picker
		  ref="entertainDateRef"
		  v-model="entertainDatePicker"
		  :formatter="formatter"
		  mode="date"
		  @confirm="entertainDateConfirm"
		/>

    <view
      class="detail-itemRequisition"
      v-for="(item, index) in ItemRequisitionList.itemRequisitionDetail"
      :key="index"
    >
      <view class="detail-item">
        <p style="color: #86909C">采购明细{{index+1}}</p>
        <p class="detail-utils">
          <text @tap="copyDetailItem">复制</text>
          <text @tap="deleteDetailItem(index)" v-if="ItemRequisitionList.itemRequisitionDetail.length > 1">删除</text>
        </p>
      </view>

      <view class="picker-content">
        <view class="measurement-info-list">
          <text class="info-list-left isrequired">物品名称</text>
          <view class="info-list-right">
            <view class="info-content">
              <input
                adjust-position
                type="text"
                class="uni-input"
                placeholder="请输入"
                v-model="item.goodsName"
              />
            </view>
          </view>
        </view>
      </view>
			<view class="picker-content">
			  <view class="measurement-info-list">
			    <text class="info-list-left isrequired">型号或规格</text>
			    <view class="info-list-right">
			      <view class="info-content">
			        <input
			          adjust-position
			          type="text"
			          class="uni-input"
			          placeholder="请输入"
			          v-model="item.goodsModel"
			        />
			      </view>
			    </view>
			  </view>
			</view>
      <view class="picker-content">
        <view class="measurement-info-list">
          <text class="info-list-left isrequired">数量</text>
          <view class="info-list-right">
            <view class="info-content">
              <input
                adjust-position
                type="text"
                class="uni-input"
                placeholder="请输入"
                v-model="item.goodsNumber"
              />
            </view>
          </view>
        </view>
      </view>
      <view class="picker-content">
        <view class="measurement-info-list">
          <text class="info-list-left isrequired">单位</text>
          <view class="info-list-right">
            <view class="info-content">
              <input
                adjust-position
                type="text"
                class="uni-input"
                placeholder="请输入"
                v-model="item.goodsUnit"
              />
            </view>
          </view>
        </view>
      </view>
			
			<view class="picker-content">
			  <view class="measurement-info-list">
			    <text class="info-list-left isrequired">单价</text>
			    <view class="info-list-right">
			      <view class="info-content" style="display: flex; align-items: center">
			        <input
			          adjust-position
			          type="text"
			          class="uni-input"
			          placeholder="请输入"
			          v-model="item.goodsUnitPrice"
			        />
			        <p style="font-size: 28rpx; line-height: 1.4em">元</p>
			      </view>
			    </view>
			  </view>
			</view>
			
			<view class="picker-content">
			  <view class="measurement-info-list">
			    <text class="info-list-left isrequired">金额</text>
			    <view class="info-list-right">
			      <view class="info-content" style="display: flex; align-items: center">
			        <input
			          adjust-position
			          type="text"
			          class="uni-input"
			          placeholder="请输入"
			          v-model="item.goodsMoney"
			        />
			        <p style="font-size: 28rpx; line-height: 1.4em">元</p>
			      </view>
			    </view>
			  </view>
			</view>
			
      <view class="textarea-box">
        <text class="info-list-left isrequired">采购用途</text>
        <textarea
          v-model="item.goodsPurpose"
          class="textinfo-list"
          placeholder="请输入"
        ></textarea>
      </view>

    </view>
		
    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">采购总金额</text>
        <view class="info-list-right">
          <view class="info-content" style="display: flex; align-items: center">
            <input
              adjust-position
              type="text"
              class="uni-input"
              placeholder="请输入"
              v-model="ItemRequisitionList.purchaseMoney"
            />
            <p style="font-size: 28rpx; line-height: 1.4em">元</p>
          </view>
        </view>
      </view>
    </view>
		
		<view class="textarea-box">
		  <text class="info-list-left">备注</text>
		  <textarea
		    v-model="ItemRequisitionList.remark"
		    class="textinfo-list"
		    placeholder="请输入"
		  ></textarea>
		</view>

    <!--    说明附件-->
    <DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit"/>
    <!--    审批流程-->
    <AuditApplicationSteps :ruleId="'caigou'" :purchaseType="purchaseTypeNumParams" :purchaseMoney="ItemRequisitionList.purchaseMoney"/>
    <!--    底部占位符-->
    <view class="blank"></view>
    <!--    保存-->
    <view class="buttonContainer">
      <button class="buttonClass" @click="clickSave">提交</button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

.bukaSvg {
  width: 32rpx;
  height: 32rpx;
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
  align-items: center;
  padding: 16rpx;
  background-color: #fff;
}

.buttonClass {
  display: flex;
  width: 686rpx;
  height: 72rpx;
  justify-content: center;
  align-items: center;
  background: #165dff;
  border-radius: 16rpx;
  color: #fff;
  font-size: 32rpx;
  //margin-top: 40rpx;
  overflow: hidden;
}

.uni-inputs {
  color: #86909c !important;
}

.uni-input {
  text-align: right;
  font-size: 28rpx;
}

.blank {
  height: calc(72rpx + 32rpx + 20rpx);
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.bukabanci {
  .info-content {
    color: #86909c !important;
  }

  .info-list-left {
    // @include font-color-auxiliary;
    color: #86909c !important;
  }
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.popup-content {
  background: #f7f8fa;

  .header-box {
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    p {
      color: #1d2129;
      font-size: 34rpx;
      font-weight: 500;
    }

    .module-image-ba {
      border-radius: 32rpx;
      background: #f2f3f5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }

  .card-list {
    display: flex;
    align-items: center;
    flex-direction: column;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .list {
      box-shadow: 0 4rpx 16rpx 0 #0000000d;
      box-sizing: border-box;
      width: 100%;
      background: #fff;
      padding: 16rpx 24rpx;
      border-radius: 8rpx;
      margin-bottom: 10rpx;

      h4 {
        color: #1d2129;
        font-size: 32rpx;
        font-weight: 400;
      }

      p {
        color: #86909c;
        font-size: 24rpx;
        font-weight: 400;
        margin-top: 10rpx;
      }
    }
  }
}

.detail-itemRequisition{
  .detail-item{
  padding: 10rpx 30rpx;
    @include flex($align: center, $justify: space-between);

    .detail-utils{
      @include flex($align: center, $justify: space-between);
      width: 150rpx;
      color: #165DFF;
    }


  }
  .text-info{
    color: #86909C;
    padding: 20rpx 30rpx;
    display: inline-block;
  }
}


.measurement-info-list {
  @include flex($align: center, $justify: space-between);
  padding: $spacing-row-base;
  background: #fff;
  margin-top: 16rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .info-list-right {
    @include flex($align: center);

    .info-content {
      @include textStyleBase;
      font-size: 34rpx;
    }

    .iconfont {
      margin-left: 16rpx;
      // @include font-color-auxiliary;
      color: $uni-fc-fu-lin;
    }
  }

  .unit {
    // @include font-color-auxiliary;
    color: $uni-fc-fu-lin;
  }
}
</style>
